﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class catalog : BlogBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        

        foreach (Category cat in Category.Categories)
        {
            string name = cat.Title;
            List<Post> list = cat.Posts.FindAll(delegate(Post p) { return p.IsVisible; });

            //HtmlGenericControl h2 = CreateRowHeader(cat, name, list.Count);
            //h2.Style.Add(HtmlTextWriterStyle.TextAlign, "center");
            //phCatalog.Controls.Add(h2);


            StringBuilder sb = new StringBuilder();
            sb.Append("<div id=\"bigcontbox\"><div class=\"box_title\"><div class='title'><b>");
            sb.Append(cat.Title);
            sb.Append("</b></div></div><div class=\"box_cont\"><div class=\"list\"><ul>");
            
            foreach (Post var in list)
            {
                sb.Append("<li style=\"width:33%;\">");

                sb.Append(string.Format("<a href=\"{0}\">{1}</a>", var.RelativeLink, var.Title));
                sb.Append(" </li>");

            }
            sb.Append("</ul></div></div></div>");

            LiteralControl lc = new LiteralControl(sb.ToString());
            phCatalog.Controls.Add(lc);






            //HtmlTable table = CreateTable(name);
            //int i = 0;
            //HtmlTableRow row = null;
            //foreach (Post post in list)
            //{
            //    if ( i%3 == 0)
            //    {
            //       row  = new HtmlTableRow();
            //    }

            //    HtmlTableCell title = new HtmlTableCell();
            //    title.InnerHtml = string.Format("<a href=\"{0}\">{1}</a>", post.RelativeLink, post.Title);
            //    title.Attributes.Add("class", "title");
            //    row.Cells.Add(title);
            //    ++i;

            //    if (i%3 ==0)
            //    {
            //        table.Rows.Add(row);
            //    }
            //}
            //if (i%3 !=0)
            //{
            //    for (int j = 0; j < 3- i % 3; ++j)
            //    {
            //        row.Cells.Add((new HtmlTableCell()));
            //    }
            //    table.Rows.Add(row);
            //}
            //phCatalog.Controls.Add(table);
        }

    }

    private static HtmlGenericControl CreateRowHeader(Category cat, string name, int count)
    {
        HtmlGenericControl h2 = new HtmlGenericControl("h2");
        h2.Attributes["id"] = Utils.RemoveIllegalCharacters(name);

        Control header = new LiteralControl(name + " (" + count + ")");
        h2.Controls.Add(header);
        return h2;
    }

    private static void CreateTableRow(HtmlTable table, Post post)
    {
        HtmlTableRow row = new HtmlTableRow();

        HtmlTableCell title = new HtmlTableCell();
        title.InnerHtml = string.Format("<a href=\"{0}\">{1}</a>", post.RelativeLink, post.Title);
        title.Attributes.Add("class", "title");
        row.Cells.Add(title);

        table.Rows.Add(row);
    }

    private HtmlTable CreateTable(string name)
    {
        HtmlTable table = new HtmlTable();
        table.Style["width"] = "100%";

        return table;
    }


}
